package com.oig.sys.auth.controller;


import com.oig.common.module.SysResponse;
import com.oig.common.validator.GroupAdd;
import com.oig.common.validator.GroupEdit;
import com.oig.sys.auth.dto.PermissionDto;
import com.oig.sys.auth.service.ITbPermissionService;
import com.oig.sys.auth.service.ITbUserRoleService;
import com.oig.sys.auth.vo.PermissionVo;
import com.oig.sys.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "6-接口权限标识符管理")
@RestController
@RequestMapping("/permission")
@Slf4j
public class TbPermissionController {

    private final ITbPermissionService permissionService;
    private final ITbUserRoleService userRoleService;

    @Autowired
    public TbPermissionController(ITbPermissionService permissionService,ITbUserRoleService userRoleService){
        this.permissionService = permissionService;
        this.userRoleService = userRoleService;
    }

    @PreAuthorize("@pms.hasPermission('/permission/addPermission')")
    @SysLog(value="添加权限",apiUrl="/permission/addPermission")
    @Operation(summary = "添加权限,这里定义权限为系统接口")
    @PostMapping("/addPermission")
    public PermissionVo addPermission(@RequestBody @Validated(GroupAdd.class) PermissionDto permissionDto){
        log.info("add permissionDto:{}",permissionDto.toString());
        return permissionService.addPermission(permissionDto) ;
    }

    @PreAuthorize("@pms.hasPermission('/permission/updatePermission')")
    @SysLog(value="修改接口信息",apiUrl="/permission/updatePermission")
    @Operation(summary = "修改接口信息")
    @PostMapping("/updatePermission")
    public PermissionVo updatePermission(@RequestBody @Validated(GroupEdit.class) PermissionDto permissionDto){
        log.info("update permissionDto:{}",permissionDto.toString());
        return permissionService.updatePermission(permissionDto) ;
    }

    @PreAuthorize("@pms.hasPermission('/permission/cancelPermission')")
    @SysLog(value="逻辑删除接口信息",apiUrl="/permission/cancelPermission")
    @Operation(summary = "逻辑删除接口信息,逻辑删除接口信息")
    @PostMapping(value = "/cancelPermission/{permissionId}")
    public SysResponse<Integer> cancelPermission(@PathVariable("permissionId") Integer permissionId){
        log.info("cancel permissionId:{}",permissionId);
        this.permissionService.cancelPermission(permissionId);
        return SysResponse.ok(permissionId,"删除成功");
    }


    @Operation(summary = "查看,查看详情，如果有子表则包含子表信息")
    @PostMapping(value = "/info/{permissionId}")
    public PermissionVo info(@PathVariable("permissionId") Integer permissionId){
        log.info("info PermissionVo id={}", permissionId) ;
        return this.permissionService.getPermissionInfo(permissionId);
    }


    @Operation(summary = "获取用户权限")
    @PostMapping(value = "/loadPermission/{userId}")
    public List<PermissionVo> loadPermissionByUserId(@PathVariable("userId") Integer userId){
        log.info("loadPermissionByUserId id={}", userId) ;
        return this.userRoleService.findAllOkPermissionVo(userId) ;
    }
}
